<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CommandTogetherGameCount extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $connect = 'api';
    protected $activity = 'activity';

    protected $signature = 'CommandTogetherGameCount';

    protected $togetherGamePeopleConfig = 'together_game_people_config';//查询统计记录配置表
    protected $togetherGamePeopleStaticts = 'together_game_people_statistics';//每日游戏统计表

    
     /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每天凌晨获取昨天各个游戏统计';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle(){
        $startDay = Carbon::yesterday()->startOfDay()->toDateTimeString();
        $endDay = Carbon::yesterday()->endOfDay()->toDateTimeString();
        //查询要统计的数据
        $selectData = DB::connection($this->connect)->table($this->togetherGamePeopleConfig)->get();
        $selectData = objToArray($selectData);
        do{
            if(empty($selectData)){
                break;
            }
            foreach($selectData as $v){
                if($v['type'] == 1){
                    $count = DB::connection($this->activity)->table($v['db_table_name'])->selectRaw(DB::raw('COUNT(DISTINCT(user_id)) as count'))->where('created_at','>=',$startDay)->where('created_at','<=',$endDay)->first();
                    $count = objToArray($count);
                    $joinUsers = DB::connection($this->activity)->table($v['db_table_name'])->selectRaw(DB::raw("user_id"))->where('created_at','>=',$startDay)->where('created_at','<=',$endDay)->groupBy('user_id')->limit(5)->get();
                    $joinUsers = objToArray($joinUsers);
                    if(empty($joinUsers)){
                        $joinUsers = 0;
                    }else{
                        $joinUsers = array_column($joinUsers,'user_id');
                        $joinUsers = implode(',',$joinUsers);
                    }
                   
                    //入库
                    $data = [
                        'game_name' => $v['game_name'],
                        'people_count' => $count['count'],
                        'created_at' => Carbon::now()->toDateTimeString(),
                        'join_users' => $joinUsers
                    ];
                    DB::connection($this->connect)->table($this->togetherGamePeopleStaticts)->insert($data);
                }elseif($v['type'] == 2){
                    $count = DB::connection($this->connect)->table($v['db_table_name'])->selectRaw(DB::raw('COUNT(DISTINCT(user_id)) as count'))->where('created_at','>=',$startDay)->where('created_at','<=',$endDay)->first();
                    $count = objToArray($count);
                    $joinUsers = DB::connection($this->connect)->table($v['db_table_name'])->selectRaw(DB::raw("user_id"))->where('created_at','>=',$startDay)->where('created_at','<=',$endDay)->groupBy('user_id')->limit(5)->get();
                    $joinUsers = objToArray($joinUsers);
                    if(empty($joinUsers)){
                        $joinUsers = 0;
                    }else{
                        $joinUsers = array_column($joinUsers,'user_id');
                        $joinUsers = implode(',',$joinUsers);
                    }
                    //入库
                    $data = [
                        'game_name' => $v['game_name'],
                        'people_count' => $count['count'],
                        'created_at' => Carbon::now()->toDateTimeString(),
                        'join_users' => $joinUsers
                    ];
                    DB::connection($this->connect)->table($this->togetherGamePeopleStaticts)->insert($data);
                }else{
                    break;
                }
            }
        }while(false);
    }
}